package com.sweetcircle.controller;

import com.sweetcircle.annotation.CheckObject;
import com.sweetcircle.annotation.CheckValue;
import com.sweetcircle.annotation.GlobalInterceptor;
import com.sweetcircle.domain.enums.VerifyRegexEnum;
import com.sweetcircle.domain.po.UserInfo;
import com.sweetcircle.domain.vo.ResponseVO;
import com.sweetcircle.domain.vo.SysSettingVO;
import com.sweetcircle.domain.vo.UserInfoLoginVO;
import com.sweetcircle.domain.vo.UserInfoVO;
import com.sweetcircle.exception.BusinessException;
import com.sweetcircle.service.UserInfoService;
import com.sweetcircle.utils.*;
import com.wf.captcha.ArithmeticCaptcha;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController("accountController")
@RequestMapping("/account")
public class AccountController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private RedisComponet redisComponet;

    /**
     * 验证码
     */
    @GetMapping(value = "/captcha")
    public ResponseVO captcha() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
        String code = captcha.text();
        String captchaKey = AnanUtils.getId();
        redisComponet.savaCaptcha(code, captchaKey);
        String captchaBase64 = captcha.toBase64();
        Map<String, String> result = new HashMap<>();
        result.put("captchaBase64", captchaBase64);
        result.put("captchaKey", captchaKey);
        return successResponseVO(result);
    }

    @PostMapping(value = "/register")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO register(@CheckObject(required = true, checkProperties = {"password", "email", "captchaKey", "captcha", "nickName"}) @RequestBody UserInfoLoginVO userInfoLoginVO) {
        String code = redisComponet.getCaptcha(userInfoLoginVO.getCaptchaKey());
        if (code == null) {
            throw new BusinessException(400, "验证码已失效");
        }
        if (!(userInfoLoginVO.getCaptcha().equalsIgnoreCase(code))) {
            throw new BusinessException(400, "验证码错误");
        }
        userInfoService.register(userInfoLoginVO);


        return successResponseVO("注册成功");
    }


    @PostMapping(value = "/login")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO login(@CheckObject(required = true, checkProperties = {"password", "email", "captchaKey", "captcha"}) @RequestBody UserInfoLoginVO userInfoLoginVO, HttpServletRequest request) {
        String captchaKey = userInfoLoginVO.getCaptchaKey();
        String code = redisComponet.getCaptcha(captchaKey);
        if (code == null) {
            throw new BusinessException(400, "验证码已失效");
        }
        if (!(userInfoLoginVO.getCaptcha().equalsIgnoreCase(code))) {
            throw new BusinessException(400, "验证码错误");
        }
        userInfoLoginVO.setToken(AnanUtils.getAuthorization(request));
        HashMap<String, Object> login = userInfoService.login(userInfoLoginVO);
        return successResponseVO("登录成功", login);
    }

    @PostMapping(value = "/resetPassword")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO resetPassword(@CheckObject(required = true, checkProperties = {"password", "email", "captchaKey", "captcha"}) @RequestBody UserInfoLoginVO userInfoLoginVO) {
        String code = redisComponet.getCaptcha(userInfoLoginVO.getCaptchaKey());
        if (code == null) {
            throw new BusinessException(400, "验证码已失效");
        }
        if (!(userInfoLoginVO.getCaptcha().equalsIgnoreCase(code))) {
            throw new BusinessException(400, "验证码错误");
        }
        userInfoService.resetPassword(userInfoLoginVO);


        return successResponseVO("密码重置成功");
    }


    @RequestMapping(value = "/getSysSetting")
    @GlobalInterceptor
    public ResponseVO getSysSetting() {
        return successResponseVO(AnanUtils.copyBean(redisComponet.getSysSetting(), SysSettingVO.class));
    }

    @PostMapping("/contactList")
    @GlobalInterceptor(checkParams = true,checkLogin = true)
    public ResponseVO testGetAllAccounts(@CheckValue(required = true)@RequestBody String userId) {
        ArrayList<HashMap<String, Object>> allContact = userInfoService.getAllContact(userId);
        return successResponseVO(allContact);
    }

    @PostMapping("/getUserInfo")
    @GlobalInterceptor(checkLogin = true, checkParams = true)
    public ResponseVO getUserInfo(@CheckValue(required = true) @RequestBody HashMap<String, String> params) {

        UserInfo userInfo;
        String id = params.get("id");
        if (VerifyUtils.verify(VerifyRegexEnum.EMAIL, id)) {

            userInfo = userInfoService.getUserInfoByEmail(id);
        } else {
            userInfo = userInfoService.getUserInfoByuserSweetCircleId(id);
        }
        if (userInfo == null) {
            return successResponseVO(400, "无法找到该用户，请你检查账号或者邮箱是否正确。");
        } else {
            return successResponseVO(AnanUtils.copyBean(userInfo, UserInfoVO.class));
        }

    }

    @PostMapping(value = "/updateUserInfo")
    @GlobalInterceptor(checkParams = true,checkLogin = true)
    public ResponseVO updateUserInfo(@CheckObject(required = true, checkProperties = {"avatar", "userId", "userSweetCircleId", "nickName", "sex", "joinType", "areaName", "personalSignature"}) @RequestBody UserInfoVO userInfoVO, HttpServletRequest request) {

        userInfoVO.setToken(AnanUtils.getAuthorization(request));
        userInfoService.updateUserInfo(userInfoVO);
        return successResponseVO("信息修改成功");
    }


}
